package com.efuture.androidmvpdemo.login
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.view.Window
import android.view.WindowManager
import com.efuture.androidmvpdemo.databinding.ActivityLoginBinding
import com.efuture.androidmvpdemo.main.MainActivity
import com.efuture.androidmvpdemo.register.RegitserActivity
import com.efuture.androidmvpdemo.ui.base.BaseActivity
import kotlinx.coroutines.MainScope

class LoginActivity : BaseActivity(),LoginView {
    private val mainScope = MainScope()
    private var binding: ActivityLoginBinding? = null
    private val presenter = LoginPresenter(this, LoginInteractor())
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //关键代码
        binding = ActivityLoginBinding.inflate(layoutInflater)
        val rootView: View = binding!!.getRoot()
        setContentView(rootView)

        click()


    }

    private fun click(){

        binding?.btLogin?.setOnClickListener {
            presenter.login(mainScope,binding?.etUsername?.text.toString(),binding?.etPassword?.text.toString())
        }
        binding?.btRegister?.setOnClickListener {
            startActivity(Intent(this@LoginActivity,RegitserActivity::class.java))
        }

    }

    override fun onDestroy() {
        super.onDestroy()
        presenter.onDestroyView(mainScope)

    }

    override fun loginSuccess() {
        ToastUtils.showShort("登录成功")
        startActivity(Intent(this@LoginActivity,MainActivity::class.java))
    }

    override fun loginFail(reason:String) {
        ToastUtils.showShort(reason)
    }

    override fun clearInputData() {
        if(binding?.etUsername?.text?.length!! >0){
            binding?.etUsername?.setText("")
        }
        if(binding?.etPassword?.text?.length!! >0){
            binding?.etPassword?.setText("")
        }
    }

}